package org.ant.MyCalculate;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class calculateMenu extends Activity {
	private boolean flag = false;
	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	String operation = "";
	public void setOperation(String operation) {
		this.operation = operation;
	}

	Double firstNumber = 0.0;
	public void setFirstNumber(Double firstNumber) {
		this.firstNumber = firstNumber;
	}
	public void setSecondNumber(Double secondNumber) {
		this.secondNumber = secondNumber;
	}

	Double secondNumber ;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calculatemenu);
		final Button button00 = (Button) findViewById(R.id.Button00);
		final Button button01 = (Button) findViewById(R.id.Button01);
		final Button button02 = (Button) findViewById(R.id.Button02);
		final Button button03 = (Button) findViewById(R.id.Button03);
		final Button button04 = (Button) findViewById(R.id.Button04);
		final Button button05 = (Button) findViewById(R.id.Button05);
		final Button button06 = (Button) findViewById(R.id.Button06);
		final Button button07 = (Button) findViewById(R.id.Button07);
		final Button button08 = (Button) findViewById(R.id.Button08);
		final Button button09 = (Button) findViewById(R.id.Button09);
		final Button buttonMinus = (Button) findViewById(R.id.ButtonMinus);
		final Button buttonAdd = (Button) findViewById(R.id.ButtonAdd);
		final Button buttonDouble = (Button) findViewById(R.id.ButtonDouble);
		final Button buttonDiv = (Button) findViewById(R.id.ButtonDiv);
		final Button buttonEqu = (Button) findViewById(R.id.ButtonEqu);
		final Button buttonClear = (Button) findViewById(R.id.ButtonClear);
		final Button buttonPoint = (Button) findViewById(R.id.ButtonPoint);
		button00.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				appearContextOnText(button00);
			}
		});
		button01.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				appearContextOnText(button01);
			}
		});
		button02.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				appearContextOnText(button02);
			}
		});
		button03.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				appearContextOnText(button03);
			}
		});
		button04.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				appearContextOnText(button04);
			}
		});
		button05.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				appearContextOnText(button05);
			}
		});
		button06.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				appearContextOnText(button06);
			}
		});
		button07.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				appearContextOnText(button07);
			}
		});
		button08.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				appearContextOnText(button08);
			}
		});
		button09.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				appearContextOnText(button09);
			}
		});
		buttonAdd.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				appearContextOnText(buttonAdd);
			}
		});
		buttonMinus.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				appearContextOnText(buttonMinus);
			}
		});
		buttonDouble.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				appearContextOnText(buttonDouble);
			}
		});
		buttonDiv.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				appearContextOnText(buttonDiv);
			}
		});
		buttonEqu.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				appearContextOnText(buttonEqu);
			}
		});
		buttonClear.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				clearContext(buttonClear);
			}
		});
		buttonPoint.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				dealWithPoint(buttonPoint);
			}
		});
	}
	private void dealWithPoint(Button button){
		TextView textView = (TextView) findViewById(R.id.TextView01);
		PointTool pointTool = new PointTool(button ,textView ,flag);
		pointTool.dealPoint(button ,textView);
	}
	private void clearContext(Button button){
		TextView textView = (TextView) findViewById(R.id.TextView01);
		textView.setText("0.0");
		flag = false;
	}
	private void appearContextOnText(Button button) {
		TextView textView = (TextView) findViewById(R.id.TextView01);
		AppearNumberTool appearNumberTool = new AppearNumberTool(button ,textView ,firstNumber ,secondNumber ,flag ,operation);
		HashMap<String, Object> map = appearNumberTool.dealNumber();
		dealWithMap(map);
	}
	
	private void dealWithMap(HashMap<String, Object> hashMap){
		if(hashMap.containsKey("textView")){
			TextView newView = (TextView) hashMap.get("textView");
			TextView textView = (TextView) findViewById(R.id.TextView01);
			textView.setText(newView.getText());
		}
		if(hashMap.containsKey("flag")){
			Boolean newFlag = (Boolean) hashMap.get("flag");
			setFlag(newFlag);
		}
		if(hashMap.containsKey("operation")){
			String newoperation = (String) hashMap.get("operation");
			setOperation(newoperation);
		}
		if(hashMap.containsKey("firstNumber")){
			Double firstNumber = (Double) hashMap.get("firstNumber");
			setFirstNumber(firstNumber);
		}
		if(hashMap.containsKey("secondNumber")){
			Double secondNumber = (Double) hashMap.get("secondNumber");
			setSecondNumber(secondNumber);
		}
	}
}
